<template>
  <view>
    <view class="sort" @click="sortComment">
      <view :class="{active:scoreType===-1}" mark:scoreType="-1">全部({{commentCount.all}})</view>
      <view :class="{active:scoreType===10}" mark:scoreType="10">好评({{commentCount.praise}})</view>
      <view :class="{active:scoreType===20}" mark:scoreType="20">中评({{commentCount.review}})</view>
      <view :class="{active:scoreType===30}" mark:scoreType="30">差评({{commentCount.negative}})</view>
    </view>
    <view v-if="commentList[0]" class="comment">
      <CommentItem :data="commentList" timePosition="right"></CommentItem>
      <view class="footer">亲，没有更多了</view>
    </view>
    <view v-else class="no-comment">暂无相关评论哦~</view>

    <view v-if="showBack" class="toTop" @click="backToTop">
      <uni-icons type="plusempty" size="30" color="#f8f8f8"></uni-icons>
    </view>
  </view>
</template>

<script>
import CommentItem from '@/components/CommentItem'
export default {
  components:{
    CommentItem
  },
  data(){
    return {
      goodsId:0,
      scoreType:-1,
      commentCount:{},
      commentList:[],
      showBack:false,
    }
  },
  async onLoad({gid}){
    this.goodsId = gid
    await this.getCommentCount()
    await this.getCommentList()
  },
  onPageScroll(e){
    if (e.scrollTop > 800) {
      this.showBack = true
    } else {
      this.showBack = false
    }
  },
  methods:{
    sortComment(e){
      if (e.mark.scoreType){
        this.scoreType = +e.mark.scoreType
        this.getCommentList()
      }
    },
    async getCommentList(){
      const {data} = await this.$http.get('/comment/list',{
        scoreType: this.scoreType,
        goodsId: this.goodsId,
        page: 1
      })
      this.commentList = data.list.data
    },
    async getCommentCount(){
      const {data} = await this.$http.get('/comment/total',{
        goodsId: this.goodsId,
      })
      this.commentCount = data.total
    },
    backToTop(){
      wx.pageScrollTo({
        scrollTop:0,
        duration:500
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .sort {
    display: flex;
    justify-content: space-around;
    font-size: 28rpx;
    .active {
      position: relative;
      font-weight: 700;
      color: red;
      &::after {
        content: '';
        display: block;
        position: absolute;
        bottom: -30%;
        left: 50%;
        transform: translateX(-50%);
        width: 50%;
        height: 15%;
        border-radius: 20rpx;
        background-color: red;
      }
    }
  }
  .comment {
    padding-top: 50rpx;
    .footer {
    margin:0;
    height: 100rpx;
    background-color: #fafafa;
    text-align: center;
    line-height: 100rpx;
    font-size: 28rpx;
    color:#808080;  
    }
  }
  .no-comment {
    margin: 300rpx auto;
    text-align: center;
  }
  .toTop {
    position: fixed;
    bottom: 15%;
    right: 5%;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100rpx;
    height: 100rpx;
    background-color: #b2b2b2;
    border-radius: 50%;
  }
</style>